<template>
  <div class="app-container">
    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleChange"
          v-hasPermi="['resource:info:add']"
        >替换素材</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['resource:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['resource:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['resource:info:export']"
        >导出</el-button>
      </el-col> -->
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    <!-- </el-row> -->

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="屏幕号" align="center" prop="id" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleGet(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >替换</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  <el-dialog :visible.sync="uploadOpen" width="500px" :destroy-on-close="true">
      <!-- 下拉框 -->
      <el-select v-model="id" disabled>
        <el-option
          v-for="item in screens"
          :label="item + '号屏幕'"
          :value="item"
          :key="item"
        >
        </el-option>
      </el-select>
      <common-upload style="margin: 20px 0;" type="image" :url="fileUploadUrl + id" width="400px" />
  </el-dialog>
  </div>

</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/resource/info";
import CommonUpload from '@/components/CommonUpload/index.vue';
import axios from 'axios';
import { THEME_UPLOAD_URL } from '@/utils/constant.js';
export default {
  name: "ThemeManager",
  components: {
    CommonUpload
  },
  data() {
    return {
      // 文件上传框打开
      uploadOpen: false,
      // 上传文件类型,
      fileUploadUrl: THEME_UPLOAD_URL,
      // 遮罩层
      loading: false,
      // 屏幕数组
      screens: [1, 2, 3],
      // 欲操作的屏幕id
      id: 1,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资源信息表格数据
      infoList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        screenNumber: null,
        hasResource: null,
        resourceType: null
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      // this.loading = true;
      // let promises = [
      //   axios('http://116.204.109.57:8081/screensave/1'),
      //   axios('http://116.204.109.57:8081/screensave/2'),
      //   axios('http://116.204.109.57:8081/screensave/3')
      // ]
      // Promise.allSettled(promises).then(res => {
      //   console.log(res);
      //   this.total = 3;
      //   this.loading = false;
      // }).catch(err => {
      //   this.loading = false;
      // })
      this.infoList = Array.of(1, 2, 3).map(val => {
        return { "id": val }
      })
      this.total = 9;
      // listInfo(this.queryParams).then(response => {
      //   this.infoList = response.rows;
      //   this.total = 3;
      //   this.loading = false;
      // }).catch(err => {
      //   this.loading = false;
      // });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        screenNumber: null,
        hasResource: null,
        resourceType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看屏保 */
    handleGet(row) {
      const { id } = row
      window.open(`${THEME_UPLOAD_URL}${id}`, '_blank')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const { id } = row;
      this.id = id;
      this.uploadOpen = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('resource/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
